﻿using University.Workshop.Business;
using University.Workshop.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using University.Workshop.Model;
using System.Collections.ObjectModel;

namespace University.Workshop.Tests
{
    
    
    /// <summary>
    ///This is a test class for WebmasterRequestDataAccessTest and is intended
    ///to contain all WebmasterRequestDataAccessTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WebmasterRequestDataAccessTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for FinishRequest
        ///</summary>
        [TestMethod()]
        public void FinishRequestTest()
        {
            string firstName = "Hermione";
            string lastName = "Granger";
            char gender = 'F';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "hermione" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            long candId = UserDataAccess.GetUserByEmail(email).Id;
            string skills = "I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?";
            WebmasterRequest newRequest = new WebmasterRequest { CandidateId = candId, Date = DateTime.Now, Skills = skills, NegativeVotes = 0, PositiveVotes = 0, WantsToReplyTickets = true, WantsToSetWebmasters = false, Status = WebmasterRequestStatus.Voting };
            newRequest = WebmasterRequestDataAccess.InsertNewWebmasterRequest(newRequest);
            long requestId = newRequest.Id; // TODO: Initialize to an appropriate value
            WebmasterRequest actual;
            actual = WebmasterRequestDataAccess.FinishRequest(requestId);
            Assert.AreEqual(WebmasterRequestStatus.Finished, actual.Status);
        }

        /// <summary>
        ///A test for GatherAllVotingRequests
        ///</summary>
        [TestMethod()]
        public void GatherAllVotingRequestsTest()
        {
            InsertNewWebmasterRequestTest();
            Collection<WebmasterRequest> actual;
            actual = WebmasterRequestDataAccess.GatherAllVotingRequests();
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for GetWebmasterRequestByCandidateId
        ///</summary>
        [TestMethod()]
        public void GetWebmasterRequestByCandidateIdTest()
        {
            string firstName = "Albus";
            string lastName = "Dumbledor";
            char gender = 'M';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "albus" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            long candId = UserDataAccess.GetUserByEmail(email).Id;
            string skills =
                "skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good";
            WebmasterRequest expected = WebmasterRequestManager.RegisterNewRequest(email, skills, true, true);
            WebmasterRequest actual;
            actual = WebmasterRequestDataAccess.GetWebmasterRequestByCandidateId(candId);
            Assert.AreEqual(expected.Id, actual.Id);
        }

        /// <summary>
        ///A test for GetWebmasterRequestById
        ///</summary>
        [TestMethod()]
        public void GetWebmasterRequestByIdTest()
        {
            string firstName = "Perico";
            string lastName = "De los palotes";
            char gender = 'M';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "periquito" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            long candId = UserDataAccess.GetUserByEmail(email).Id;
            string skills =
                "skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good";
            WebmasterRequest expected = WebmasterRequestManager.RegisterNewRequest(email, skills, true, true);
            long requestId = expected.Id; 
            WebmasterRequest actual;
            actual = WebmasterRequestDataAccess.GetWebmasterRequestById(requestId);
            Assert.AreEqual(expected.Id, actual.Id);
        }

        /// <summary>
        ///A test for HasVoted
        ///</summary>
        [TestMethod()]
        public void HasVotedTest()
        {
            string firstName = "Flaca";
            string lastName = "no me claves tus puñales";
            char gender = 'F';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "flaca" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            string skills =
                "skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good";
            WebmasterRequest newReq = WebmasterRequestManager.RegisterNewRequest(email, skills, true, true);
            long requestId = newReq.Id;
            WebmasterRequestDataAccess.RecordVote(email, requestId);
            bool expected = true;
            bool actual;
            actual = WebmasterRequestDataAccess.HasVoted(email, requestId);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for InsertNewWebmasterRequest
        ///</summary>
        [TestMethod()]
        public void InsertNewWebmasterRequestTest()
        {
            string firstName = "Ron";
            string lastName = "Weasley";
            char gender = 'M';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "ron" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            long candId = UserDataAccess.GetUserByEmail(email).Id;
            string skills = "I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?I have a lot of skills you know?";
            WebmasterRequest newRequest = new WebmasterRequest{ CandidateId = candId,Date = DateTime.Now, Skills = skills, NegativeVotes = 0, PositiveVotes = 0, WantsToReplyTickets = true, WantsToSetWebmasters = false, Status = WebmasterRequestStatus.Voting};
            WebmasterRequest expected = newRequest;
            WebmasterRequest actual;
            actual = WebmasterRequestDataAccess.InsertNewWebmasterRequest(newRequest);
            Assert.AreEqual(expected.CandidateId, actual.CandidateId);
        }

        /// <summary>
        ///A test for RecordVote
        ///</summary>
        [TestMethod()]
        public void RecordVoteTest()
        {
            string firstName = "Maria";
            string lastName = "La del barrio";
            char gender = 'F';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "maria" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            long candId = UserDataAccess.GetUserByEmail(email).Id;
            string skills =
                "skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good";
            WebmasterRequest newReq = WebmasterRequestManager.RegisterNewRequest(email, skills, true, true);
            string userEmail = email; 
            long requestId = newReq.Id;
            WebmasterRequestDataAccess.RecordVote(userEmail, requestId);
            Assert.IsNotNull(newReq);
        }

        /// <summary>
        ///A test for UpdateRequestVotes
        ///</summary>
        [TestMethod()]
        public void UpdateRequestVotesTest()
        {
            string firstName = "Paco";
            string lastName = "Pedro de la mar";
            char gender = 'M';
            Random rand = new Random();
            DateTime birthday = DateTime.Now.AddYears(-18);
            string email = "paco" + rand.Next(1000) + "@gmail.com";
            string password = "qwert1";
            string rePassword = "qwert1";
            string department = "Cochabamba";
            string city = "Cercado";
            string movilephone = "" + rand.Next(79999999);
            string phone = "" + rand.Next(4999999);
            UserRegistrationManager.RegisterUser(firstName, lastName, gender, birthday, email, password, rePassword, department, city, movilephone, phone);
            long candId = UserDataAccess.GetUserByEmail(email).Id;
            string skills =
                "skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good skills good";
            WebmasterRequest newReq = WebmasterRequestManager.RegisterNewRequest(email, skills, true, true);
            long requestId = newReq.Id; // TODO: Initialize to an appropriate value
            int extraVotes = 1; // TODO: Initialize to an appropriate value
            WebmasterRequest actual;
            actual = WebmasterRequestDataAccess.UpdateRequestVotes(requestId, extraVotes);
            Assert.AreEqual(newReq.PositiveVotes+1, actual.PositiveVotes);
        }
    }
}
